/**
 * Created by user on 2017/4/5.
 * 指令     translate
 */
angular.module(moduleName + '.directives')
	.directive('translate', function ($compile) {
		return {
			restrict: 'A',
			priority: 1001, //
			terminal: true,
			compile: function (tElement, tAttrs) {
				tElement.removeAttr('translate');
				return function (scope, element, attrs) {
					if (attrs.ngRepeat) {
						replaceRepeat(element, attrs, scope)
					} else {
						replaceAttr(element, attrs, scope)
					}
				}
			}
		};

		function replaceRepeat(element, attrs, scope) {
			var outhtml = element[0].outerHTML;
			var reg = /in\s(\w*)/;
			if (reg.test(outhtml)) {
				var bindString = reg.exec(outhtml)[1];
				scope[bindString] = obj[bindString];
				//outhtml = outhtml.replace(match,JSON.stringify(obj[match]).replace(/\"/g,"'"));
				var template = $compile(outhtml)(scope);
				element.replaceWith(template);
			}
		}

		function replaceAttr(element, attrs, scope) {
		var bindHtml = element.html();
		if(bindHtml){
		  reg = /\{{(.*)\}}/;
			if (reg.test(bindHtml)) {
				var match = reg.exec(bindHtml)[1];
				element.text(obj[match]);
			}
		}

			var attrTags = [];
			if (attrs) {
				for (attr in attrs) {
					if (attrs.hasOwnProperty(attr)) {
						attrTags.push(attr);
					}
				}
			}
			if (attrTags && attrTags.length > 0) {
				attrTags.forEach(function (attr) {
					if (obj[attrs[attr]]) {
						scope[attrs[attr]] = obj[attrs[attr]]
					}
				})
				var outhtml = element[0].outerHTML;
				var template = $compile(outhtml)(scope);
				element.replaceWith(template);
			}
			// var bindString = '',reg = '';
			// if(attrs.ngBind){
			//   bindString = attrs.ngBind;
			//   scope[bindString] = obj[bindString];
			//   var outhtml = element[0].outerHTML;
			//   var template = $compile(outhtml)(scope);
			//   element.replaceWith(template);
			// }
			// else{
			//   bindHtml = element.html();
			//   reg = /\{{(.*)\}}/;
			//   if(reg.test(bindHtml)){
			//       var match = reg.exec(bindHtml)[1];
			//       element.text(obj[match]);
			//   }
			// }

		}
});
